package com.account.book.service.impl;

import com.account.book.config.ListTabsConfig;
import com.account.book.entity.DetailMessage;
import com.account.book.entity.ListMessage;
import com.account.book.entity.MainMessage;
import com.account.book.enums.DataStatusEnum;
import com.account.book.mapper.DetailMessageMapper;
import com.account.book.mapper.ListMessageMapper;
import com.account.book.mapper.MainMessageMapper;
import com.account.book.request.MessageReqVo;
import com.account.book.response.DetailMessageResVo;
import com.account.book.response.ListMessageResVo;
import com.account.book.response.MainMessageResVo;
import com.account.book.response.TabsMessageResVo;
import com.account.book.service.MessageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    private static final Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);
    @Autowired
    private ListTabsConfig listTabsConfig;
    @Autowired
    private DetailMessageMapper detailMessageMapper;
    @Autowired
    private ListMessageMapper listMessageMapper;
    @Autowired
    private MainMessageMapper mainMessageMapper;

    @Autowired
    private MapperFacade mapperFacade;


    /**
     * @param
     * @return @return {@link List }<{@link String }>
     * @author liang-hw
     * @description 获取tabs列表
     * @date 2024/01/13 16:48:47
     */
    @Override
    public List<TabsMessageResVo> listTabs() {
        List<TabsMessageResVo> listTabs = new ArrayList<>();
        for (String tab : listTabsConfig.getListTabs()) {
            TabsMessageResVo tabsMessageResVo = new TabsMessageResVo();
            tabsMessageResVo.setName(tab);
            listTabs.add(tabsMessageResVo);
        }
        return listTabs;
    }

    /**
     * @param @param tabId
     * @return @return {@link List }<{@link MainMessageResVo }>
     * @author liang-hw
     * @description 获取轮播列表
     * @date 2024/01/13 18:22:14
     */
    @Override
    public List<MainMessageResVo> getMainMessage(String tabId) {
        QueryWrapper<MainMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainMessage::getTabId, tabId)
                .eq(MainMessage::getStatus, DataStatusEnum.ENABLE.getValue())
                .orderByDesc(MainMessage::getCreateTime);
        List<MainMessage> mainMessageList = mainMessageMapper.selectList(queryWrapper);
        List<MainMessageResVo> mainMessageResVoList = mapperFacade.mapAsList(mainMessageList, MainMessageResVo.class);
        return mainMessageResVoList;
    }

    /**
     * @param @param tabId
     * @return @return {@link List }<{@link ListMessageResVo }>
     * @author liang-hw
     * @description 获取列表
     * @date 2024/01/13 18:22:28
     */
    @Override
    public List<ListMessageResVo> getListMessage(MessageReqVo messageReqVo) {
        QueryWrapper<ListMessage> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(messageReqVo.getFiltrationValue())) {
            queryWrapper.lambda().like(ListMessage::getTitle, messageReqVo.getFiltrationValue());
        } else {
            queryWrapper.lambda().eq(ListMessage::getTabId, messageReqVo.getTabId());
        }
        queryWrapper.lambda().eq(ListMessage::getStatus, DataStatusEnum.ENABLE.getValue())
                .orderByDesc(ListMessage::getCreateTime);
        List<ListMessage> listMessages = listMessageMapper.selectList(queryWrapper);
        List<ListMessageResVo> listMessageResVos = mapperFacade.mapAsList(listMessages, ListMessageResVo.class);
        return listMessageResVos;
    }

    /**
     * @param @param messageReqVo
     * @return
     * @author liang-hw
     * @description 更新列表
     * @date 2024/01/13 19:10:42
     */
    @Override
    public void addListMessage(MessageReqVo messageReqVo) {
        UpdateWrapper<ListMessage> listMessageUpdateWrapper = new UpdateWrapper<>();
        listMessageUpdateWrapper.lambda()
                .eq(ListMessage::getId, messageReqVo.getListId())
                .set(ListMessage::getViewNum, messageReqVo.getViewNum());
        listMessageMapper.update(null, listMessageUpdateWrapper);
    }

    /**
     * @param @param tabId
     * @return @return {@link DetailMessageResVo }
     * @author liang-hw
     * @description 获取列表详情
     * @date 2024/01/13 18:22:40
     */
    @Override
    public DetailMessageResVo getDetailMessage(String listId) {
        QueryWrapper<DetailMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DetailMessage::getListId, listId);
        DetailMessage detailMessage = detailMessageMapper.selectOne(queryWrapper);
        DetailMessageResVo detailMessageResVo = mapperFacade.map(detailMessage, DetailMessageResVo.class);
        return detailMessageResVo;
    }

}
